/**
 * Created by enroll on 2018/6/24.
 */
import { HttpService, HTTPMethods } from "http-service";
import { Injectable } from "@angular/core";
import { API } from "../../models/api-path-extension";
import { ResponseData } from "../../models/response-data";
import { Observable } from "rxjs/Observable";
import { ResponseDistrict, ResponseDistricts } from "../../models/response/response-districts";
import { ResponseUsers } from "../../models/response/response-user";
import { ResponseWorkOrders } from "../../models/response/response-work-order";
import { SystemUser } from "../../models/system-user";
import { CurdService } from "../../models/services/curd.service";
import { UserDto } from "../../models/dto/user.dto";
import { User } from "../../models/transform/user";
import { DistrictDto } from "../../models/dto/district.dto";
import { District } from "../../models/transform/district";
import { SysUserDto } from "../../models/dto/sysUser.dto";
import { SysUser } from "../../models/transform/sysUser";
import { WorkOrderDto } from "../../models/dto/work-orders.dto";
import { WorkOrder } from "../../models/transform/work-order";
import { StorageDto } from "../../models/dto/storage.dto";
import { CustomStorage } from "../../models/transform/custom-storage";
import { BillDto } from "../../models/dto/bill.dto";
import { Bill } from "../../models/transform/bill";
import { AreaDto } from "../../models/dto/area.dto";
import { Area } from "../../models/transform/area";
import { DeviceDto } from "../../models/dto/device.dto";
import { Device } from "../../models/transform/device";
import { ObjectDto } from "../sqliteDB/object.dto";
import { SyncUtilService } from "./sync.util.service";
import { UpdateUserService } from "./update-user.service";

@Injectable()
export class UpdateBillService {
  customStorageData: Array<object>;

  constructor(
    private httpService: HttpService,
    public syncUtilService: SyncUtilService,
    public updateUserService: UpdateUserService,
    public curdService: CurdService) {


  }

  getPayHistory() {
    this.syncUtilService.fetchAllData(null, new API().path({ path: "payHistory", method: HTTPMethods.Get }))
      .map((paidPaymentResponse: ResponseData) => {
        let payments = paidPaymentResponse.data as Array<object>;
        // this.updateUserService.updateUser(userPayments);
        payments.forEach((payment) => {

          payment["isSettle"] = 1;
          payment["userId"] = payment["customerNo"];
        });
        paidPaymentResponse.data = payments;
        this.updateBill(paidPaymentResponse.data);
      }).subscribe(() => {
        // TODO
      });
  }

  // 更新成缴费
  updateBill(result) {
    result.forEach((item, index) => {
      this.curdService.fetchByCondition(BillDto, {
        and: {
          userId: item.userId,
          year: item.year,
          month: item.month
        }
      }).then((res) => {
        item = Object.assign(Array.from(res["results"])[0] || {}, item);
        item["isSettle"] = 1;
        this.curdService.update({
          dto: BillDto,
          mapObj: Bill
        }, item, {
            and: {
              userId: item.userId,
              year: item.year,
              month: item.month
            }
          }).then((affect) => {
            // 更新工单
            this.curdService.update({
              dto: WorkOrderDto,
              mapObj: WorkOrder
            }, {
                workOrderStatus: "recoverWaitSubmit"
              }, {
                and: {
                  chargeDetailId: item.chargeDetailId,
                  userId: item.userId
                }
              }).then(() => {
                // TODO
              });
          });
      });

    });
  }


}
